package com.sc.base.common.spring;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sc.base.common.utils.JacksonUtils;
import com.sc.base.common.utils.OkHttpUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

/**
 * @Author lw
 * @Date 2017/5/15 15:03
 */
@EnableHystrix
@MapperScan(basePackages = {"com.sc.**.mapper"})
@ComponentScan({"com.sc.**.service", "com.sc.**.fallback", "com.sc.**.component"})
public class BaseConfiguration{

    @Bean
    public ApplicationConstant applicationConstant() {
        return new ApplicationConstant();
    }

    //customize object mapper
    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        return JacksonUtils.OBJECT_MAPPER;
    }

    @Bean
    public ApplicationContextHolder applicationContextHolder(ApplicationConstant applicationConstant) {
        ApplicationContextHolder applicationContextHolder = ApplicationContextHolder.getInstance();
        applicationContextHolder.init(applicationConstant);
        return applicationContextHolder;
    }


    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MappingJackson2HttpMessageConverter(objectMapper());
    }

    @Primary
    @Bean
    public okhttp3.OkHttpClient okHttpClient(ApplicationConstant applicationConstant){
        return OkHttpUtils.okHttpClientBuilder(applicationConstant).build();
    }





}